using System;
using System.Drawing;
using System.Resources;

namespace Minesweeper02
{
	/// <summary>
	/// Summary description for MineResourceSet.
	/// </summary>
	public class MineResourceSet
	{
		private string filename="Minesweeper.resources";
		//
		//there is two files contains data 
		//1:Minesweeper.resources contain image and value of level
		//2:Play.resources contain data about the game like besttime,lastest property ..etc
		//
		private string beginnerBestTime;
		private string beginnerPlayerName;
		private string intermediateBestTime;
		private string intermediatePlayerName;
		private string expertBestTime;
		private string expertPlayerName;
		private string lastestLevel;
		private string lastestColumns;
		private string lastestRows;
		private string lastestMines;
		private string mark;
		private string isContinue;
		public MineResourceSet(string _filename)
		{
			//
			// TODO: Add constructor logic here
			//
			filename=_filename;
		}
		public Image[] GetMineStatus(){
			ResourceSet rs=new ResourceSet(filename);
			//filename=Minesweeper.resources
			Image[] arrimg=new Image[15];
			arrimg[0]=(System.Drawing.Image)(rs.GetObject("nomine0.Image"));
			arrimg[1]=(System.Drawing.Image)(rs.GetObject("nomine1.Image"));
			arrimg[2]=(System.Drawing.Image)(rs.GetObject("nomine2.Image"));
			arrimg[3]=(System.Drawing.Image)(rs.GetObject("nomine3.Image"));
			arrimg[4]=(System.Drawing.Image)(rs.GetObject("nomine4.Image"));
			arrimg[5]=(System.Drawing.Image)(rs.GetObject("nomine5.Image"));
			arrimg[6]=(System.Drawing.Image)(rs.GetObject("nomine6.Image"));
			arrimg[7]=(System.Drawing.Image)(rs.GetObject("nomine7.Image"));
			arrimg[8]=(System.Drawing.Image)(rs.GetObject("nomine8.Image"));
			arrimg[9]=(System.Drawing.Image)(rs.GetObject("nolabel.Image"));
			arrimg[10]=(System.Drawing.Image)(rs.GetObject("minelabel.Image"));
			arrimg[11]=(System.Drawing.Image)(rs.GetObject("undefinemine.Image"));
			arrimg[12]=(System.Drawing.Image)(rs.GetObject("mineclicked.Image"));
			arrimg[13]=(System.Drawing.Image)(rs.GetObject("nominecheckmine.Image"));
			arrimg[14]=(System.Drawing.Image)(rs.GetObject("mineuncheck.Image"));
			rs.Close();
			return arrimg;
		}
		public Image[] GetNumber(){
			ResourceSet rs=new ResourceSet(filename);
			//filename=Minesweeper.resources
			Image[] arrimg=new Image[11];
			arrimg[0]=(System.Drawing.Image)(rs.GetObject("number0.Image"));
			arrimg[1]=(System.Drawing.Image)(rs.GetObject("number1.Image"));
			arrimg[2]=(System.Drawing.Image)(rs.GetObject("number2.Image"));
			arrimg[3]=(System.Drawing.Image)(rs.GetObject("number3.Image"));
			arrimg[4]=(System.Drawing.Image)(rs.GetObject("number4.Image"));
			arrimg[5]=(System.Drawing.Image)(rs.GetObject("number5.Image"));
			arrimg[6]=(System.Drawing.Image)(rs.GetObject("number6.Image"));
			arrimg[7]=(System.Drawing.Image)(rs.GetObject("number7.Image"));
			arrimg[8]=(System.Drawing.Image)(rs.GetObject("number8.Image"));
			arrimg[9]=(System.Drawing.Image)(rs.GetObject("number9.Image"));
			arrimg[10]=(System.Drawing.Image)(rs.GetObject("sub.Image"));
            rs.Close();
			return arrimg;		
		}
		public Image[] GetFace(){
			ResourceSet rs=new ResourceSet(filename);
			//filename=Minesweeper.resources
			Image[] arrimg=new Image[5];
			arrimg[0]=(System.Drawing.Image)(rs.GetObject("nomal.Image"));
			arrimg[1]=(System.Drawing.Image)(rs.GetObject("clicking.Image"));
			arrimg[2]=(System.Drawing.Image)(rs.GetObject("down.Image"));
			arrimg[3]=(System.Drawing.Image)(rs.GetObject("miss.Image"));
			arrimg[4]=(System.Drawing.Image)(rs.GetObject("win.Image"));
            rs.Close();
			return arrimg;
		}
		private void ConstructorPlay(){
			ResourceSet rs=new ResourceSet(filename);
			//filename=Play.resources
			beginnerBestTime=rs.GetString("Beginner.BestTime");
			beginnerPlayerName=rs.GetString("Beginner.PlayerName");
			intermediateBestTime=rs.GetString("Intermediate.BestTime");
			intermediatePlayerName=rs.GetString("Intermediate.PlayerName");
			expertBestTime=rs.GetString("Expert.BestTime");
			expertPlayerName=rs.GetString("Expert.PlayerName");
			lastestLevel=rs.GetString("Lastest.Level");
			lastestColumns=rs.GetString("Lastest.Columns");
            lastestRows=rs.GetString("Lastest.Rows");
			lastestMines=rs.GetString("Lastest.Mines");
			mark=rs.GetString("Mark");
			isContinue=rs.GetString("IsContinue");
			rs.Close();
		}
		private void SavePlay(){
			ResourceWriter rw=new ResourceWriter(filename);
			//filename=Play.resources
			rw.AddResource("Beginner.BestTime",beginnerBestTime);
			rw.AddResource("Beginner.PlayerName",beginnerPlayerName);
			rw.AddResource("Intermediate.BestTime",intermediateBestTime);
			rw.AddResource("Intermediate.PlayerName",intermediatePlayerName);
			rw.AddResource("Expert.BestTime",expertBestTime);
			rw.AddResource("Expert.PlayerName",expertPlayerName);
			rw.AddResource("Lastest.Level",lastestLevel);
			rw.AddResource("Lastest.Columns",lastestColumns);
			rw.AddResource("Lastest.Rows",lastestRows);
			rw.AddResource("Lastest.Mines",lastestMines);
			rw.AddResource("Mark",mark);
			rw.AddResource("IsContinue",isContinue);
            rw.Generate();
			rw.Close();
		}
		public string[] LoadAttributeStartGame(){
            //filename=Play.resources
            this.ConstructorPlay();
			string[] str=new string[7];
            str[0]=lastestLevel;
			str[1]=lastestColumns;
			str[2]=lastestRows;
			str[3]=lastestMines;
			str[4]=mark;
			ResourceSet rs=new ResourceSet(filename);
			switch(str[0])
			{
				case "Beginner":
					str[5]=rs.GetString("Beginner.BestTime");
					str[6]=rs.GetString("Beginner.PlayerName");
					break;
				case "Intermediate":
					str[5]=rs.GetString("Intermediate.BestTime");
					str[6]=rs.GetString("Intermediate.PlayerName");
					break;
				case "Expert":
					str[5]=rs.GetString("Expert.BestTime");
					str[6]=rs.GetString("Expert.PlayerName");
					break;
				default:
					str[5]="";
                    str[6]="";
					break;
			}
			rs.Close();
			return str;
			
		}
		public string[] LoadGameLevel(string _level){
			ResourceSet rs=new ResourceSet(filename);
			//filename=Minesweeper.resources
			string[] str=new string[5];
			str[0]=rs.GetString(_level+".Columns");
			str[1]=rs.GetString(_level+".Rows");
			str[2]=rs.GetString(_level+".Mines");
			rs.Close();
            return str;
		}
		public string[] LoadBestTime(){
            
			this.ConstructorPlay();
			string[] str=new string[6];
			str[0]=beginnerBestTime;
			str[1]=beginnerPlayerName;
			str[2]=intermediateBestTime;
			str[3]=intermediatePlayerName;
			str[4]=expertBestTime;
			str[5]=expertPlayerName;
			return str;
		}
		public void SaveBestTime(string _level,string _besttime,string _playername){
		    this.ConstructorPlay();
			switch(_level){
				case "Beginner":
                    this.beginnerBestTime=_besttime;
					this.beginnerPlayerName=_playername;
					break;
				case "Intermediate":
					this.intermediateBestTime=_besttime;
					this.intermediatePlayerName=_playername;
					break;
				case "Expert":
					this.expertBestTime=_besttime;
					this.expertPlayerName=_playername;
					break;
				default:
					break;
			}
			this.SavePlay();
		}
		public void ResetDefaultBestTime(){
			this.ConstructorPlay();
			beginnerBestTime="999";
			beginnerPlayerName="Anonymous";
			intermediateBestTime="999";
			intermediatePlayerName="Anonymous";
			expertBestTime="999";
			expertPlayerName="Anonymous";
			this.SavePlay();
		}
		public void SaveAttribute(string _level,int _columns,int _rows,int  _mines,bool _mark){
            this.ConstructorPlay();
			lastestLevel=_level;
			lastestColumns=_columns.ToString();
			lastestRows=_rows.ToString();
			lastestMines=_mines.ToString();
			if (_mark)
			{
				mark="1";
			}
			else{
			    mark="0";
			}
		    this.SavePlay();
		}
		public bool IsContinue(){
			this.ConstructorPlay();
			return isContinue=="1";
		} 
		public void SetIsContinue(bool iscontinue){
			this.ConstructorPlay();
			if (iscontinue)
			{   
				isContinue="1";
			}
			else{
			    isContinue="0";
			}
		    this.SavePlay();
		}
	}
}
